package sharetime.core.pager;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class PagerFactory {

	@SuppressWarnings("unchecked")
	public static Pager getPager(Pager pagerOne, Pager pagerTwo) {
		if (pagerOne != null && pagerTwo != null) {
			List pagers = new ArrayList();
			pagers.add(pagerOne);
			pagers.add(pagerTwo);
			return getPager(pagers);
		}
		if (pagerOne != null)
			return pagerOne;
		if (pagerTwo != null)
			return pagerTwo;
		else
			return new DefaultPager();
	}

	@SuppressWarnings("unchecked")
	public static Pager getPager(List pagers) {
		Iterator it = pagers.iterator();
		do {
			if (!it.hasNext())
				break;
			Pager pager = (Pager) it.next();
			if (pager == null || pager.isEmpty())
				it.remove();
		} while (true);
		if (pagers.size() == 0)
			return new DefaultPager();
		if (pagers.size() == 1)
			return (Pager) pagers.get(0);
		else
			return new MergedListPager(pagers);
	}
}
